package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.util.HttpRequestHelper;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: yygh_parent
 * @author: Mr.Zhuo
 * @create: 2022-03-11 22:24
 **/
@Api("医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("排班信息删除")
    @PostMapping("schedule/remove")
    public Result scheduleRemove(HttpServletRequest request) {
        Map<String, Object> paramOB = HttpRequestHelper.switchMap(request.getParameterMap());

        // 然后进行密药检查
        String sign = (String) paramOB.get("sign");
        String signDB = hospitalSetService.findSignKeyByHoscode(paramOB.get("hoscode"));

        // 然后判断是否一致
        // 然后查询数据库中key值是否一致
        if (!sign.equals(MD5.encrypt(signDB))) {
            // 不一致
            throw new YyghException(20001, "校验码错误");
        }
        String hoscode = (String) paramOB.get("hoscode");
        String hosScheduleId = (String) paramOB.get("hosScheduleId");

        // 然后调用方法进行删除
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    @ApiOperation("排版信息查询")
    @PostMapping("schedule/list")
    public Result listSchedule(HttpServletRequest request) {
        Map<String, Object> paramOB = HttpRequestHelper.switchMap(request.getParameterMap());

        // 然后进行密药检查
        String sign = (String) paramOB.get("sign");
        String signDB = hospitalSetService.findSignKeyByHoscode(paramOB.get("hoscode"));

        // 然后判断是否一致
        // 然后查询数据库中key值是否一致
        if (!sign.equals(MD5.encrypt(signDB))) {
            // 不一致
            throw new YyghException(20001, "校验码错误");
        }
        // 进行数据转换
        String depcode = (String) paramOB.get("depcode");
        Integer page = StringUtils.isEmpty(paramOB.get("page")) ? 1 : Integer.parseInt((String) paramOB.get("page"));
        Integer limit = StringUtils.isEmpty(paramOB.get("limit")) ? 1 : Integer.parseInt((String) paramOB.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setDoccode(depcode);
        // 进行分页条件查询
        Page<Schedule> pageSchedule = scheduleService.list(page, limit, scheduleQueryVo);
        return Result.ok(pageSchedule);
    }

    @ApiOperation("存储排班信息")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 接受数据
        Map<String, Object> paramOB = HttpRequestHelper.switchMap(request.getParameterMap());

        // 密钥检查
        String sign = (String) paramOB.get("sign");
        String signDB = hospitalSetService.findSignKeyByHoscode(paramOB.get("hoscode"));

        // 然后判断是否一致
        // 然后查询数据库中key值是否一致
        if (!sign.equals(MD5.encrypt(signDB))) {
            // 不一致
            throw new YyghException(20001, "校验码错误");
        }

        // 然后调用方法进行保存数据
        scheduleService.saveSchedule(paramOB);

        return Result.ok();
    }

    @ApiOperation("删除科室")
    @PostMapping("department/remove")
    public Result remover(HttpServletRequest request) {

        // 接受数据
        Map<String, Object> paramOB = HttpRequestHelper.switchMap(request.getParameterMap());

        // 然后进行密钥检查
        String sign = (String) paramOB.get("sign");
        String signDB = hospitalSetService.findSignKeyByHoscode(paramOB.get("hoscode"));

        // 然后判断是否一致
        // 然后查询数据库中key值是否一致
        if (!sign.equals(MD5.encrypt(signDB))) {
            // 不一致
            throw new YyghException(20001, "校验码错误");
        }

        // 然后进行删除数据
        departmentService.remove(paramOB);
        return Result.ok();
    }

    @ApiOperation(value = "分页查询科室")
    @PostMapping("department/list")
    public Result listDepartment(HttpServletRequest request) {
        // 接受数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramOB = HttpRequestHelper.switchMap(parameterMap);

        // 进行密钥检查
        String sign = (String) paramOB.get("sign");
        String signDB = hospitalSetService.findSignKeyByHoscode(paramOB.get("hoscode"));

        // 然后判断是否一致
        // 然后查询数据库中key值是否一致
        if (!sign.equals(MD5.encrypt(signDB))) {
            // 不一致
            throw new YyghException(20001, "校验码错误");
        }
        String depcode = (String) paramOB.get("depcode");
        // 当前页
        int page = StringUtils.isEmpty(paramOB.get("page")) ? 1 : Integer.parseInt((String) paramOB.get("page"));
        // 每页显示数据
        int limit = StringUtils.isEmpty(paramOB.get("limit")) ? 10 : Integer.parseInt((String) paramOB.get("limit"));
        // 条件
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode((String) paramOB.get("hoscode"));
        departmentQueryVo.setDepcode(depcode);
        // 然后进行分页条件查询
        Page<Department> page1 = departmentService.findDepartmentByQueryAndLimit(page, limit, departmentQueryVo);

        return Result.ok(page1);
    }

    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 接受数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramOB = HttpRequestHelper.switchMap(parameterMap);

        // 密钥检查
        String sign = (String) paramOB.get("sign");
        String signDB = hospitalSetService.findSignKeyByHoscode(paramOB.get("hoscode"));

        // 然后判断是否一致
        // 然后查询数据库中key值是否一致
        if (!sign.equals(MD5.encrypt(signDB))) {
            // 不一致
            throw new YyghException(20001, "校验码错误");
        }

        // 然后调用方法进行保存数据
        departmentService.saveDepartment(paramOB);

        return Result.ok();
    }


    @ApiOperation(value = "查询医院")
    @PostMapping("hospital/show")
    public Result show(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> parameterOb = HttpRequestHelper.switchMap(parameterMap);

        // 然后进行密钥检查
        String sign = (String) parameterOb.get("sign");
        StringBuilder str = new StringBuilder();
        String signDB = hospitalSetService.findSignKeyByHoscode(parameterOb.get("hoscode"));
        String timestamp = (String) parameterOb.get("timestamp");
        str.append((String) parameterOb.get("hoscode")).append("|").append(timestamp).append("|").append(signDB);

        // 然后判断是否一致
        // 然后查询数据库中key值是否一致
        if (!sign.equals(MD5.encrypt(str.toString()))) {
            // 不一致
            throw new YyghException(20001, "校验码错误");
        }

        // 然后进行数据查询
        Hospital hospital = hospitalService.findHospitalByHoscode(parameterOb.get("hoscode"));


        return Result.ok(hospital);
    }

    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 获取医院那端的数据
        Map<String, String[]> parameterMap = request.getParameterMap();

        // 但是获取的数据并不是Hospital类型的 所以我们需要进行数据分析
        Map<String, Object> hospitalMap = HttpRequestHelper.switchMap(parameterMap);

        // 因为不是所有的医院都可以随意注册，只有我们审核批准之后才可以注册
        // 首先得到传输过来的sgin数据
        String sign = (String) hospitalMap.get("sign");
        String signDB = hospitalSetService.findSignKeyByHoscode(hospitalMap.get("hoscode"));

        // 然后判断是否一致
        // 然后查询数据库中key值是否一致
        if (!sign.equals(MD5.encrypt(signDB))) {
            // 不一致
            throw new YyghException(20001, "校验码错误");
        }

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) hospitalMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        hospitalMap.put("logoData", logoData);

        // 最终保存数据
        hospitalService.saveHospital(hospitalMap);

        return Result.ok().code(200);
    }
}
